package com.itboyst.face.utils;


import com.itboyst.face.exception.CodeEnum;
import com.itboyst.face.exception.EnumInterface;

import java.io.Serializable;

/**
 * 返回数据
 */
public class Result<T> extends BaseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public Result() {
    }

    public Result(boolean success, String msg) {
        this.setSuccess(success);
        this.setMessage(msg);
    }

    public Result(boolean success) {
        this.setSuccess(success);
    }

    public Result(int code, String msg) {
        this.setCode(code);
        this.setMessage(msg);
    }

    public Result(int code, String msg, T data) {
        this.setCode(code);
        this.setMessage(msg);
        this.setData(data);
    }

    public static Result ok() {
        return ok(CodeEnum.SUCCESS);
    }

    public static Result ok(String msg) {
        return ok(CodeEnum.SUCCESS.getCode(), msg);
    }

    public static Result ok(EnumInterface info) {
        return ok(info.getCode(), info.getMsg());
    }

    public static <T> Result<T> ok(int code, String msg) {
        return baseCreate(code, msg, true);
    }

    public static Result fail() {
        return fail(CodeEnum.ERROR);
    }

    public static Result fail(String msg) {
        return fail(CodeEnum.ERROR.getCode(), msg);
    }

    public static Result fail(EnumInterface info) {
        return fail(info.getCode(), info.getMsg());
    }

    public static Result fail(int code, String msg) {
        return baseCreate(code, msg, false);
    }

    private static <T> Result<T> baseCreate(int code, String msg, boolean success) {
        Result result = new Result();
        result.setCode(code);
        result.setSuccess(success);
        result.setMessage(msg);
        result.setTimestamp(DateTimeUtil.getSysTime());
        return result;
    }

    public Result<T> setResult(T data) {
        this.setData(data);
        return this;
    }

    public T getData() {
        return (T) super.getData();
    }

}
